const axios = require('axios');
const fs = require('fs');
const path = require('path');

async function testTableOfContents() {
    try {
        console.log('开始测试目录生成功能...');
        
        // 读取测试数据
        const testData = JSON.parse(fs.readFileSync(path.join(__dirname, 'backend', 'data.json'), 'utf8'));
        
        console.log('发送请求到createDocx接口...');
        const response = await axios.post('http://localhost:3000/api/createDocx', testData, {
            responseType: 'arraybuffer',
            headers: {
                'Content-Type': 'application/json'
            }
        });
        
        console.log('接收到响应，保存文档...');
        
        // 保存生成的Word文档
        const outputPath = path.join(__dirname, 'test-document-with-toc.docx');
        fs.writeFileSync(outputPath, response.data);
        
        console.log(`✅ 文档已生成并保存到: ${outputPath}`);
        console.log('请打开文档查看目录功能是否正常工作');
        
    } catch (error) {
        console.error('❌ 测试失败:', error.message);
        if (error.response) {
            console.error('响应状态:', error.response.status);
            console.error('响应数据:', error.response.data.toString());
        }
    }
}

testTableOfContents();